package com.xiaoshuidi.cloud.module.contract.controller.admin.contract;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.DateRangeGenerator;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.OrderResp;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.SettlementOrderFeeVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.rentOrder.RentOrderVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.RentingOutRoomsRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.*;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.company.*;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.settlement.ContractCompanyBatchSettlementVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.settlement.ContractSettementVO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.contract.CompanyContractService;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractSettlementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.groups.Default;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 企业合同")
@RestController
@RequestMapping(ApiConstants.NAME + "/company-contract")
@Validated
public class CompanyContractController {

    @Resource
    private CompanyContractService companyContractService;
    @Resource
    private ContractSettlementService contractSettlementService;

    @PostMapping("/create")
    @Operation(summary = "创建企业合同")
    //@PreAuthorize("@ss.hasPermission('contract:company-contract:create')")
    public CommonResult<Long> createCompanyContract(@Valid @RequestBody CompanyContractCreateReqVO createReqVO) {
        return success(companyContractService.createCompanyContract(createReqVO));
    }

    @GetMapping("/page")
    @Operation(summary = "企业合同分页查询")
    //@PreAuthorize("@ss.hasPermission('contract:company-contract:create')")
    public CommonResult<PageResult<CompanyContractListRespVO>> pageCompanyContract(@Valid CompanyContractPageReqVO pageReqVO) {
        return success(companyContractService.pageCompanyContract(pageReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "企业合同通过id查询")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('contract:company-contract:create')")
    public CommonResult<CompanyContractRentDataRespVO> getCompanyContract(@RequestParam("id") Long id) {
        return success(companyContractService.get(id));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出企业合同合同 Excel")
    //@PreAuthorize("@ss.hasPermission('contract:owner:export')")
    @OperateLog(type = EXPORT)
    public void exportCompanyContractExcel(@Valid CompanyContractExcelReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<CompanyContractListExcelVO> list = companyContractService.getExportCompanyContractList(exportReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "企业合同.xls", "数据", CompanyContractListExcelVO.class, list);
    }

    @GetMapping("/list-simple")
    @Operation(summary = "企业客户中的合同管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('contract:company-contract:create')")
    public CommonResult<CompanyContractStatusListRespVO> getCompanyContractSimpleList(@RequestParam(
            "customerCompanyId") Long customerCompanyId) {
        return success(companyContractService.getCompanyContractSimpleList(customerCompanyId));
    }

    /**
     * 企业合同批量退租
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/create-refund")
    @Operation(summary = "企业合同-退租")
    //@PreAuthorize("@ss.hasPermission('contract:company-contract::createExtension')")
    public CommonResult createExtension(@Validated(value = {Default.class, ContractRefundReqVO.BathInsert.class}) @RequestBody ContractRefundReqVO reqVO) {
        companyContractService.createCompanyRefund(reqVO);
        return success();
    }

    @PostMapping("/get-room-order")
    @Operation(summary = "获取房间账单")
    public CommonResult<List<RentingOutRoomsRespVO>> getRoomOrder(@RequestBody CompanyContractRentOutRoomReqVO reqVO){
        return success(companyContractService.getRoomOrder(reqVO.getCompanyContractId(),reqVO.getActualRentOutDate()));
    }

    /**
     * 企业合同小合同续租
     * @param createReqVO
     * @return
     */
    @PostMapping("/create-renewal-sub")
    @Operation(summary = "创建租客合同-续租")
//    @PreAuthorize("@ss.hasPermission('contract:createRenewal')")
    public CommonResult createRenewal(@Valid @RequestBody ContractRenewalReqVO createReqVO) {
        companyContractService.createRenewalContractCompanySub(createReqVO);
        return success();
    }

    /**
     * 企业合同小合同退租
     * @param reqVO
     * @return
     */
    @PostMapping("/create-refund-sub")
    @Operation(summary = "创建租客合同-退租")
//    @PreAuthorize("@ss.hasPermission('contract:createRenewal')")
    public CommonResult createRefund(@Valid @RequestBody ContractRefundReqVO reqVO) {
        companyContractService.createRefund(reqVO, Boolean.TRUE);
        return success();
    }

    /**
     * 企业合同续租
     * @param reqVO
     * @return
     */
    @PostMapping("/create-renewal")
    @Operation(summary = "创建企业合同合同-续租")
//    @PreAuthorize("@ss.hasPermission('contract:company-contract:createRenewal')")
    public CommonResult createRenewal(@Valid @RequestBody CompanyContractRenewalReqVO reqVO) {
        companyContractService.createRenewalContractCompany(reqVO);
        return success();
    }
    /**
     * 获取企业房间列表
     * @param companyContractId
     * @return
     */
    @GetMapping("/get-rooms")
    @Operation(summary = "获取企业合同所有生效的房间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('contract:company-contract:create')")
    public CommonResult<List<CompanyContractRoomsVO>> getCompanyRooms(@RequestParam("companyContractId") Long companyContractId){
        return success(companyContractService.getCompanyRooms(companyContractId));
    }

    /**
     * 企业续租退租、续租租客信息
     *
     * @param id 企业合同id
     * @return
     */
    @GetMapping("/get-room-data")
    @Operation(summary = "企业续租退租、续租租客信息")
    @Parameter(name = "id", description = "企业合同id", required = true, example = "1024,2048")
//    @PreAuthorize("@ss.hasPermission('contract:query')")
    public CommonResult<ContractTenantInformationRepVO> getByCompanyContract(@RequestParam("id") Long id) {
        return success(companyContractService.getRentByContractId(id));
    }

    /**
     * 获取企业合同可以续租的房间列表
     * @param companyContractId
     * @return
     */
    @GetMapping("/get-renewal-rooms")
    @Operation(summary = "获取企业合同可以续租的房间列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('contract:company-contract:create')")
    public CommonResult<List<CompanyContractRoomsVO>> getCompanyRenewalRooms(@RequestParam("companyContractId") Long companyContractId){
        return success(companyContractService.getCompanyRenewalRooms(companyContractId));
    }

    @PostMapping("/generate")
    @Operation(summary = "生成企业预览账单")
    public CommonResult<OrderResp> generateBills(@Valid @RequestBody RentOrderVO rentOrderVO) {
                return success(companyContractService.getNewBillPreview(rentOrderVO));
    }

    /**
     * 退租结算单
     */
    @GetMapping("/batch-settlement")
    @Operation(summary = "退租生成结算单")
//    @PreAuthorize("@ss.hasPermission('contract:export')")
    public CommonResult<BatchSettlementOrderFeeVO> contractSettlement(@Valid ContractSettementVO contractSettementVO) {
        BatchSettlementOrderFeeVO settlement = contractSettlementService.batchSettlement(contractSettementVO.getIds(), contractSettementVO.getEndTime(), contractSettementVO.getSettlementType());
        return success(settlement);
    }

    /**
     * 企业合同取消签约
     */
    @GetMapping("/cancel")
    public CommonResult<Void> cancelSigning(@Valid @RequestParam("id") Long id) {
        companyContractService.cancelSigning(id);
        return success();
    }

    /**
     * 企业批量退租清算单详情
     */
    @GetMapping("/batch-settlement/detail")
    public CommonResult<ContractCompanyBatchSettlementVO> getCompnayBatchSettlementById(@RequestParam("id") Long id) {
        return success(companyContractService.getCompnayBatchSettlementById(id));
    }
}
